package com.clouddms.service.transfer;

import java.util.HashSet;
import java.util.Set;

/**
 * 上传会话信息
 * 
 * @author Cloud-DMS Team
 * @version 1.0.0
 */
public class UploadSession {
    private String uploadId;
    private String fileName;
    private long fileSize;
    private Long userId;
    private int totalChunks;
    private int chunkSize;
    private Set<Integer> completedChunks;
    private long uploadedBytes;
    private UploadStatus status;
    private long createdTime;
    private long lastUpdateTime;
    private long completedTime;
    private String tempDirectory;
    private String finalFilePath;
    private String errorMessage;

    public UploadSession() {
        this.completedChunks = new HashSet<>();
        this.uploadedBytes = 0;
        this.status = UploadStatus.INITIALIZED;
    }

    // === 业务方法 ===

    /**
     * 标记分块完成
     */
    public void markChunkCompleted(int chunkIndex) {
        completedChunks.add(chunkIndex);
    }

    /**
     * 检查是否所有分块都已完成
     */
    public boolean isAllChunksCompleted() {
        return completedChunks.size() == totalChunks;
    }

    /**
     * 获取上传进度百分比
     */
    public double getUploadProgress() {
        if (totalChunks == 0) return 0.0;
        return (double) completedChunks.size() / totalChunks * 100.0;
    }

    /**
     * 增加已上传字节数
     */
    public void addUploadedBytes(long bytes) {
        this.uploadedBytes += bytes;
    }

    /**
     * 获取剩余未上传的分块数
     */
    public int getRemainingChunks() {
        return totalChunks - completedChunks.size();
    }

    /**
     * 检查分块是否已完成
     */
    public boolean isChunkCompleted(int chunkIndex) {
        return completedChunks.contains(chunkIndex);
    }

    /**
     * 获取上传耗时（毫秒）
     */
    public long getUploadDuration() {
        if (completedTime > 0) {
            return completedTime - createdTime;
        } else {
            return System.currentTimeMillis() - createdTime;
        }
    }

    /**
     * 计算平均上传速度（字节/秒）
     */
    public double getAverageUploadSpeed() {
        long duration = getUploadDuration();
        if (duration <= 0) return 0.0;
        return (double) uploadedBytes / (duration / 1000.0);
    }

    // === Getters and Setters ===

    public String getUploadId() {
        return uploadId;
    }

    public void setUploadId(String uploadId) {
        this.uploadId = uploadId;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public int getTotalChunks() {
        return totalChunks;
    }

    public void setTotalChunks(int totalChunks) {
        this.totalChunks = totalChunks;
    }

    public int getChunkSize() {
        return chunkSize;
    }

    public void setChunkSize(int chunkSize) {
        this.chunkSize = chunkSize;
    }

    public Set<Integer> getCompletedChunks() {
        return completedChunks;
    }

    public void setCompletedChunks(Set<Integer> completedChunks) {
        this.completedChunks = completedChunks;
    }

    public long getUploadedBytes() {
        return uploadedBytes;
    }

    public void setUploadedBytes(long uploadedBytes) {
        this.uploadedBytes = uploadedBytes;
    }

    public UploadStatus getStatus() {
        return status;
    }

    public void setStatus(UploadStatus status) {
        this.status = status;
    }

    public long getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(long createdTime) {
        this.createdTime = createdTime;
    }

    public long getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(long lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public long getCompletedTime() {
        return completedTime;
    }

    public void setCompletedTime(long completedTime) {
        this.completedTime = completedTime;
    }

    public String getTempDirectory() {
        return tempDirectory;
    }

    public void setTempDirectory(String tempDirectory) {
        this.tempDirectory = tempDirectory;
    }

    public String getFinalFilePath() {
        return finalFilePath;
    }

    public void setFinalFilePath(String finalFilePath) {
        this.finalFilePath = finalFilePath;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    @Override
    public String toString() {
        return String.format("UploadSession{uploadId='%s', fileName='%s', progress=%.1f%%, status=%s}", 
            uploadId, fileName, getUploadProgress(), status);
    }
}